package cn.edu.nju.software;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class SwapToStandard {
	private String[] standardN = new String[]{"SIDS",
		"SID",
		"STUDNETCOURSES",
		"STUDNETCOURSE",
		"INSTITUTE",
		"COURSES",
		"COURSE",
		"CIDS",
		"CID",
		"NAME",
		"TEACHER",
		"TIME",
		"PALCE",
		"SHARABLE",
		"GRADE"};
	
	private String filePath;
	private String fromServer;
	private String standardPath;
	
	public SwapToStandard(String fromServer, String filePath) {
		this.filePath = filePath;
		this.fromServer = fromServer;
		standardPath = filePath.substring(0, 4) + "2" + filePath.substring(4);
		swap();
	}
	
	private void swap() {
		File file = new File(filePath);
		File targetFile = new File(standardPath);
		try {
			Scanner input  = new Scanner(file);
			PrintWriter output = new PrintWriter(targetFile);
			String s = "";
			
			while(input.hasNext()) {
				s += input.nextLine();
			}
			for(int i = 0; i < standardN.length; i++) {
				s = s.replaceAll(fromServer + standardN[i], standardN[i]); 
			}
			
			output.write(s);
			
			input.close();
			output.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getStandardPath() {
		return standardPath;
	}
}
